package com.wocao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.*;
import com.wocao.service.SysDictTypeService;
import com.wocao.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
public class LogOperController {


    @Autowired
    private SysOperLogService sysOperLogService;

    @Autowired
    private SysDictTypeService sysDictTypeService;


    /*分页显示：查询*/
    @PostMapping("sysOperLog-find")
    public ResponseResult<Page<SysOperLog>> findSysOperLog(@RequestBody SysDictTypeAndDateCheck checkData) {
        try {
            Page<SysOperLog> pages = sysOperLogService.getPage(new Page<>(Long.parseLong(checkData.getCurrent()), Long.parseLong(checkData.getSize()))
                    , checkData);
            /*使用xml完成多条件查询*/
            return ResponseResult.ok(pages);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, "查询失败" + e.getMessage());
        }
    }

    /*获取所有数据*/
    @PostMapping("sysOperLog-all")
    public ResponseResult<List<String>> getSysDictDataPages(){
        try{
            List<String> list = sysOperLogService.businessTypeAll();
            return new ResponseResult<>(200L,"数据查询成功",list);
        }catch (Exception e){
            return new ResponseResult<>(500L,"数据查询失败"+e.getMessage(),null);
        }
    }

    /*删除多条*/
    @PostMapping("sysOperLog-some")
    public ResponseResult<String> deleteSomeSysOperLog(@RequestBody List<Integer> ids) {
        try {
            sysOperLogService.removeByIds(ids);
            return ResponseResult.ok("ok");
        } catch (Exception e) {
            return ResponseResult.error(500L, "删除失败");
        }
    }

    /*清空*/
    @PostMapping("sysOperLog-ClearData")
//    @CacheEvict(cacheNames = "存入redis的标识名称", allEntries = true)
    public ResponseResult<String> delCache() {
        try {
            sysOperLogService.remove(new QueryWrapper<>());
            return ResponseResult.ok("清除完成！");
        } catch (Exception e) {
            return ResponseResult.error(500L, "无缓存数据可清除！");
        }
    }

    /*导出*/
    @PostMapping("sysOperLog-exportNumber")
    public ResponseResult<List<SysDictType>> exportNumber(@RequestBody java.lang.String[] ids) {
        List<SysOperLog> sysOperLog;
        try {
            if (ids == null) {
                sysOperLog = sysOperLogService.list();
            } else {
                sysOperLog = sysOperLogService.listByIds(Arrays.asList(ids));
            }
            return ResponseResult.ok(sysOperLog);
        } catch (Exception e) {
            return ResponseResult.error(500L, "获取导出数据失败" + e.getMessage());
        }
    }
}
